package cn.mxz.service.impl;

import cn.mxz.mapper.UserAddrMapper;
import cn.mxz.pojo.ResultData;
import cn.mxz.pojo.UserAddr;
import cn.mxz.service.AddrService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author 小毛
 * @Date 2021/12/2 15:39
 * @Remark 佛祖保佑永无bug~
 */
@Service
public class AddrServiceImpl implements AddrService {
    @Autowired
    private UserAddrMapper addrMapper;

    @Override
    public ResultData addrinsert(UserAddr addr) {
        addr.setCreateTime(new Date());
        addr.setUpdateTime(new Date());
        int count = addrMapper.insert(addr);
        if (count > 0) return new ResultData(0,"添加成功",addr);
        return new ResultData(301,"添加接口异常");
    }

    @Override
    public ResultData addrdelete(String addrId) {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("addr_id",addrId);
        int count = addrMapper.delete(qw);
        if (count > 0) return new ResultData(0,"删除成功");
        return new ResultData(302,"删除接口异常");
    }

    @Override
    public ResultData addrupdate(UserAddr addr) {
        addr.setUpdateTime(new Date());
        int count = addrMapper.updateById(addr);
        if (count > 0) return new ResultData(0,"修改成功",addr);
        return new ResultData(303,"修改接口异常");
    }

    @Override
    public ResultData addrlist(String userId) {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("user_id",userId);
        qw.orderByDesc("common_addr");
        List<UserAddr> addrList = addrMapper.selectList(qw);
        if (addrList.size() > 0) return new ResultData(0,"查询成功",addrList);
        return new ResultData(304,"查询接口异常");
    }

    @Override
    public ResultData addrMoRen(UserAddr addr) {
        String userId = addr.getUserId();
        int i = addrMapper.changeMoRen(userId);
        addr.setCommonAddr(1);
        int i1 = addrMapper.updateById(addr);
        if (i1 > 0 ) return new ResultData(0,"修改成功");
        return new ResultData(305,"修改接口异常");
    }

}
